<html>
<head>
<title>MyHttpUtils.ets</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.s0 { color: #080808;}
.s1 { color: #e986cd;}
.s2 { color: #653170;}
.s3 { color: #859801;}
.s4 { color: #870000; font-style: italic;}
.s5 { color: #6c71c4;}
.ln { color: #adadad; font-weight: normal; font-style: normal; }
</style>
</head>
<body bgcolor="#ffffff">
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#c0c0c0" >
<tr><td><center>
<font face="Arial, Helvetica" color="#000000">
MyHttpUtils.ets</font>
</center></td></tr></table>
<pre><a name="l1"><span class="ln">1    </span></a><span class="s0">import </span><span class="s1">{ </span><span class="s0">http </span><span class="s1">} </span><span class="s0">from </span><span class="s2">&quot;@kit.NetworkKit&quot;</span>
<a name="l2"><span class="ln">2    </span></a><span class="s0">import </span><span class="s1">{ </span><span class="s0">CollectQueryParam</span><span class="s3">, </span><span class="s0">IsCollectedResp</span><span class="s3">, </span><span class="s0">RankListItem </span><span class="s1">} </span><span class="s0">from </span><span class="s2">&quot;../model/CollectInfo&quot;</span>
<a name="l3"><span class="ln">3    </span></a><span class="s0">import Poem from </span><span class="s2">&quot;../model/Poem&quot;</span>
<a name="l4"><span class="ln">4    </span></a><span class="s0">import Constants from </span><span class="s2">&quot;./Constants&quot;</span>
<a name="l5"><span class="ln">5    </span></a>
<a name="l6"><span class="ln">6    </span></a><span class="s0">class MyHttpUtils </span><span class="s1">{</span>
<a name="l7"><span class="ln">7    </span></a>  <span class="s4">// 获取收藏数据</span>
<a name="l8"><span class="ln">8    </span></a>  <span class="s0">async getMyCollectData</span><span class="s5">(</span><span class="s0">userId</span><span class="s3">:</span><span class="s0">number</span><span class="s5">) </span><span class="s1">{</span>
<a name="l9"><span class="ln">9    </span></a>    <span class="s4">// 根据当前用户id 查询用户收藏的所有诗词数据 必传参数是userId</span>
<a name="l10"><span class="ln">10   </span></a>    <span class="s0">let res </span><span class="s3">= </span><span class="s0">await http</span><span class="s3">.</span><span class="s0">createHttp</span><span class="s5">()</span><span class="s3">.</span><span class="s0">request</span><span class="s5">(</span><span class="s0">Constants</span><span class="s3">.</span><span class="s0">myCollectApi</span><span class="s3">, </span><span class="s1">{</span>
<a name="l11"><span class="ln">11   </span></a>      <span class="s0">method</span><span class="s3">: </span><span class="s0">http</span><span class="s3">.</span><span class="s0">RequestMethod</span><span class="s3">.</span><span class="s0">POST</span><span class="s3">,</span>
<a name="l12"><span class="ln">12   </span></a>      <span class="s0">header</span><span class="s3">: </span><span class="s1">{</span>
<a name="l13"><span class="ln">13   </span></a>        <span class="s2">'Content-Type'</span><span class="s3">: </span><span class="s2">'application/json'</span>
<a name="l14"><span class="ln">14   </span></a>      <span class="s1">}</span><span class="s3">,</span>
<a name="l15"><span class="ln">15   </span></a>      <span class="s0">extraData</span><span class="s3">: </span><span class="s1">{</span>
<a name="l16"><span class="ln">16   </span></a>        <span class="s0">userId</span><span class="s3">: </span><span class="s0">userId</span>
<a name="l17"><span class="ln">17   </span></a>      <span class="s1">}</span>
<a name="l18"><span class="ln">18   </span></a>    <span class="s1">}</span><span class="s5">)</span>
<a name="l19"><span class="ln">19   </span></a>    <span class="s0">return JSON</span><span class="s3">.</span><span class="s0">parse</span><span class="s5">(</span><span class="s2">`</span><span class="s1">${</span><span class="s0">res</span><span class="s3">.</span><span class="s0">result</span><span class="s1">}</span><span class="s2">`</span><span class="s5">)</span>
<a name="l20"><span class="ln">20   </span></a>  <span class="s1">}</span>
<a name="l21"><span class="ln">21   </span></a>
<a name="l22"><span class="ln">22   </span></a>  <span class="s4">// 判断当前诗词是否被自己收藏过 驱动UI显示</span>
<a name="l23"><span class="ln">23   </span></a>  <span class="s0">async detectionFlag</span><span class="s5">(</span><span class="s0">apiStr</span><span class="s3">: </span><span class="s0">string</span><span class="s3">,</span><span class="s0">userIdAndPoemId</span><span class="s3">:</span><span class="s0">CollectQueryParam</span><span class="s5">) </span><span class="s1">{</span>
<a name="l24"><span class="ln">24   </span></a>    <span class="s4">// 发送当前诗词id 访问接口 响应boolean</span>
<a name="l25"><span class="ln">25   </span></a>    <span class="s0">let res </span><span class="s3">= </span><span class="s0">await http</span><span class="s3">.</span><span class="s0">createHttp</span><span class="s5">()</span><span class="s3">.</span><span class="s0">request</span><span class="s5">(</span><span class="s0">apiStr</span><span class="s3">,</span>
<a name="l26"><span class="ln">26   </span></a>      <span class="s1">{</span>
<a name="l27"><span class="ln">27   </span></a>        <span class="s0">header</span><span class="s3">: </span><span class="s1">{</span>
<a name="l28"><span class="ln">28   </span></a>          <span class="s2">'Content-Type'</span><span class="s3">: </span><span class="s2">'application/json'</span>
<a name="l29"><span class="ln">29   </span></a>        <span class="s1">}</span><span class="s3">,</span>
<a name="l30"><span class="ln">30   </span></a>        <span class="s0">method</span><span class="s3">: </span><span class="s0">http</span><span class="s3">.</span><span class="s0">RequestMethod</span><span class="s3">.</span><span class="s0">POST</span><span class="s3">,</span>
<a name="l31"><span class="ln">31   </span></a>        <span class="s0">extraData</span><span class="s3">: </span><span class="s0">JSON</span><span class="s3">.</span><span class="s0">stringify</span><span class="s5">(</span><span class="s0">userIdAndPoemId</span><span class="s5">)</span>
<a name="l32"><span class="ln">32   </span></a>      <span class="s1">}</span>
<a name="l33"><span class="ln">33   </span></a>    <span class="s5">)</span>
<a name="l34"><span class="ln">34   </span></a>    <span class="s0">let isCollectedResp</span><span class="s3">: </span><span class="s0">IsCollectedResp </span><span class="s3">= </span><span class="s0">JSON</span><span class="s3">.</span><span class="s0">parse</span><span class="s5">(</span><span class="s2">`</span><span class="s1">${</span><span class="s0">res</span><span class="s3">.</span><span class="s0">result</span><span class="s1">}</span><span class="s2">`</span><span class="s5">) </span><span class="s0">as IsCollectedResp</span>
<a name="l35"><span class="ln">35   </span></a>
<a name="l36"><span class="ln">36   </span></a>    <span class="s0">return isCollectedResp</span><span class="s3">.</span><span class="s0">collectFlag</span>
<a name="l37"><span class="ln">37   </span></a>  <span class="s1">}</span>
<a name="l38"><span class="ln">38   </span></a>
<a name="l39"><span class="ln">39   </span></a>  <span class="s4">// 获取今日诗词数据</span>
<a name="l40"><span class="ln">40   </span></a>  <span class="s0">async getTodayPoem</span><span class="s5">()</span><span class="s1">{</span>
<a name="l41"><span class="ln">41   </span></a>    <span class="s0">let res </span><span class="s3">= </span><span class="s0">await http</span><span class="s3">.</span><span class="s0">createHttp</span><span class="s5">()</span><span class="s3">.</span><span class="s0">request</span><span class="s5">(</span><span class="s0">Constants</span><span class="s3">.</span><span class="s0">todayPoem</span><span class="s5">)</span>
<a name="l42"><span class="ln">42   </span></a>    <span class="s0">return JSON</span><span class="s3">.</span><span class="s0">parse</span><span class="s5">(</span><span class="s2">`</span><span class="s1">${</span><span class="s0">res</span><span class="s3">.</span><span class="s0">result</span><span class="s1">}</span><span class="s2">`</span><span class="s5">) </span><span class="s0">as Poem</span>
<a name="l43"><span class="ln">43   </span></a>  <span class="s1">}</span>
<a name="l44"><span class="ln">44   </span></a>
<a name="l45"><span class="ln">45   </span></a>
<a name="l46"><span class="ln">46   </span></a>  <span class="s4">// 获取收藏榜数据</span>
<a name="l47"><span class="ln">47   </span></a>  <span class="s0">async getCollectRankData</span><span class="s5">()</span><span class="s1">{</span>
<a name="l48"><span class="ln">48   </span></a>    <span class="s0">let res </span><span class="s3">= </span><span class="s0">await http</span><span class="s3">.</span><span class="s0">createHttp</span><span class="s5">()</span><span class="s3">.</span><span class="s0">request</span><span class="s5">(</span><span class="s0">Constants</span><span class="s3">.</span><span class="s0">collectRankApi</span><span class="s3">,</span>
<a name="l49"><span class="ln">49   </span></a>      <span class="s1">{</span>
<a name="l50"><span class="ln">50   </span></a>        <span class="s0">method</span><span class="s3">: </span><span class="s0">http</span><span class="s3">.</span><span class="s0">RequestMethod</span><span class="s3">.</span><span class="s0">POST</span><span class="s3">,</span>
<a name="l51"><span class="ln">51   </span></a>        <span class="s0">header</span><span class="s3">: </span><span class="s1">{</span>
<a name="l52"><span class="ln">52   </span></a>          <span class="s2">'Content-Type'</span><span class="s3">: </span><span class="s2">'application/json'</span>
<a name="l53"><span class="ln">53   </span></a>        <span class="s1">}</span>
<a name="l54"><span class="ln">54   </span></a>      <span class="s1">}</span><span class="s5">)</span>
<a name="l55"><span class="ln">55   </span></a>   <span class="s0">return JSON</span><span class="s3">.</span><span class="s0">parse</span><span class="s5">(</span><span class="s2">`</span><span class="s1">${</span><span class="s0">res</span><span class="s3">.</span><span class="s0">result</span><span class="s1">}</span><span class="s2">`</span><span class="s5">)  </span><span class="s0">as RankListItem</span><span class="s5">[]</span>
<a name="l56"><span class="ln">56   </span></a>  <span class="s1">}</span>
<a name="l57"><span class="ln">57   </span></a><span class="s1">}</span>
<a name="l58"><span class="ln">58   </span></a>
<a name="l59"><span class="ln">59   </span></a><span class="s0">let myHttpUtils </span><span class="s3">= </span><span class="s0">new MyHttpUtils</span><span class="s5">()</span>
<a name="l60"><span class="ln">60   </span></a><span class="s0">export default myHttpUtils</span></pre>
</body>
</html>